package io.renren.modules.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.regexp.internal.RE;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.utils.R;
import io.renren.modules.redis.RedisService;
import io.renren.modules.redis.User;
import io.renren.modules.redis.UserKey;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.task.entity.testUser;
import io.renren.modules.task.model.AgvParam;
import io.renren.modules.task.model.Position;
import io.renren.modules.task.rabbitmq.MQSender;
import io.renren.modules.task.websocket.MessageModule;
import io.renren.modules.task.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.Session;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: weihongyang
 * @Date: 2019/4/24 10:34
 * @Version 1.0
 */
@RestController
@Api(description = "测试接口")
@RequestMapping("/test")
public class TestController extends AbstractController implements InitializingBean {

    @Autowired
    RedisService redisService;

    @Autowired
    MQSender sender;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MessageModule messageModule;

    @PostMapping("/mq")
    @ApiOperation(value = "测试rabbitmq", notes = "测试rabbitmq")
    public R testMq(){
        sender.send("hi rabbitMQ");
        return R.ok("hello world");
    }

    //前端接受 rabbitmq 推送的消息
    @PostMapping("/mq/qd")
    @ApiOperation(value = "测试rabbitmq前端", notes = "测试rabbitmq前端")
    public R testMqQD(){
        sender.sendss("hi rabbitMQ qd");
        return R.ok("hello world QD");
    }

    @PostMapping("/mq/topic")
    @ApiOperation(value = "测试rabbitmq/topic", notes = "测试rabbitmq/topic")
    public R testMqtopic(){
        sender.sendTopic("hi rabbitMQTopic");
        return R.ok("hello world Topic");
    }

    @PostMapping("/mq/fanout")
    @ApiOperation(value = "测试rabbitmq/fanout", notes = "测试rabbitmq/fanout")
    public R testMqFanout(){
        sender.sendFanout("hi rabbitMQFanout");
        return R.ok("hello world Fanout");
    }

    @PostMapping("/mq/headers")  //Header 模式是最严格，也是最灵活的
    @ApiOperation(value = "测试rabbitmq/headers", notes = "测试rabbitmq/headers")
    public R testMqHeaders(){
        User user = new User();
        user.setName("cx");
        user.setId("123");

        sender.sendHeaders(user);
        return R.ok("hello world entity  --header");
    }

    /**
     * 系统初始化做的事情。。。
     * */
    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @ApiOperation(value = "测试获取工作台号", notes = "测试获取工作台号")
    @PostMapping("/ce/shi")
    public R test() {
        System.out.println(getUser().getConsoleCode());
        return R.ok();
    }

    @SysLog("测试修改工作台号")
    @ApiOperation(value = "修改工作台号", notes = "修改工作台号")
    @PostMapping("modify")
    public String updateConsole() {
        SysUserEntity sysUserEntity = sysUserService.getById(7);
        //sysUserService.updateConsoleCodeByUserId();
        return "修改成功";
    }

    @PostMapping("/to/agv")
    @ApiOperation(value = "空车回库操作", notes = "空车回库操作")
    public R selectEmptyPointToAgv() throws Exception {
        String url = "http://192.168.0.124/rcs/services/rest/hikRpcService/genAgvSchedulingTask";
        String taskTyp = "F01";
        Position position = new Position();
        position.setPositionCode("A");
        position.setType("00");
        Position position1 = new Position();
        position1.setType("00");
        position1.setPositionCode("B");
        Object[] positionCodePath = {position, position1};
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode("11111");
        agvParam.setTaskTyp(taskTyp);
        agvParam.setPositionCodePath(positionCodePath);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);
        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice
        return R.ok(resultMsg);
    }

    @GetMapping("redis/get/{key}")
    @ApiOperation(value = "测试redis，get", notes = "测试redis-get")
    public R getRedis(@PathVariable("key") String key){
        User user = new User();
        user.setId("1");
        user.setName("111");
        redisService.get(UserKey.getById,key, User.class);
        return R.ok().put("user",user);
    }

    @ApiOperation(value = "测试redis，set", notes = "测试redis-set")
    @PostMapping("/redis/set/{key}")
    public R info(@PathVariable("key") String key) {
        if(redisService.set(UserKey.getById,key,"12344")){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "testWebsocketToOne", notes = "testWebsocketToOne")
    @GetMapping("/websockettest/{userId}/{msg}")
    public R testWebsocketToOne(@PathVariable("userId") String userId,@PathVariable("msg") String msg) {
        if(messageModule.sendMessageToSomeone(msg,userId)){
            return R.ok().put("status","success");
        }else {
            return R.error("发送失败");
        }
    }

    @ApiOperation(value = "testWebsocketToAll", notes = "testWebsocketToAll")
    @GetMapping("/websockettest/{msg}")
    public R testWebsocketToAll(@PathVariable("msg") String msg) throws IOException {
        if(messageModule.sendMessageToAll(msg)){
            return R.ok().put("status","success");
        }else {
            return R.error("发送失败");
        }
    }

    @ApiOperation(value = "测试testList", notes = "测试testList")
    @PostMapping("/testList")
    public R list(@RequestBody List<Map<String, Object>> params) {

//        //String s2 = params.get("a2").toString();
//        Map<String,Object> map = new HashMap<>();
//        map.put("a1","a2");
//        map.put("a2",2);
//        List<Map<String,Object>> list = new ArrayList<>();
//        list.add(map);
//        System.out.println(list);

        return R.ok().put("a1", params.get(0).get("a1")).put("a2", params.get(1).get("a1"));
    }

    @ApiOperation(value = "测试参数校验", notes = "测试参数校验")
    @PostMapping("/testValitation")
    public R list(@Valid testUser testUser){  //1.

//        //String s2 = params.get("a2").toString();
//        Map<String,Object> map = new HashMap<>();
//        map.put("a1","a2");
//        map.put("a2",2);
//        List<Map<String,Object>> list = new ArrayList<>();
//        list.add(map);
//        System.out.println(list);
        if(testUser.getUserName()==null){
            throw new RRException("用户名不能为空",500);
        }
        return R.ok().put("a1", testUser.getPhone()).put("a2", testUser.getEmail());
    }


}
